﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.IO;
using WmWebSystem.Models;
using WmWebSystem.Service;
using WmWebSystem.DbHelper;

namespace WmWebSystem.Controllers
{
    public class ManagerController : Controller
    {
        //
        // GET: /Manager/

        WmDb mydb = new WmDb();

        [Authorize(Roles = "admin,editor")]
        public ActionResult Index()
        {
            return View();
        }

        [Authorize(Roles = "admin")]
        public ActionResult ProjectSearch() 
        {
            return View();
        }

        [HttpPost]
        [Authorize(Roles = "admin")]
        public ActionResult ProjectList() 
        {
            string pno = Request.Form["projectNo"];
            DateTime sTime = Convert.ToDateTime(Request.Form["startTime"]);
            DateTime eTime = Convert.ToDateTime(Request.Form["endTime"]);
            int status = Convert.ToInt32(Request.Form["projectStatus"]);

            var projects = ProjectHelper.FindProjectBySearch(pno, sTime, eTime, status);

            return View(projects);
        }

        [Authorize(Roles = "admin")]
        public ActionResult GetBusinessRecords() 
        {
            var finances = FinanceHelper.FindAllFinancesRecord();
            ViewData["allFinances"] = finances;
            return View();
        }

        [Authorize(Roles = "admin")]
        public ActionResult AccountSearch() 
        {
            return View();
        }

        [HttpPost]
        [Authorize(Roles = "admin")]
        public ActionResult GetUserAccount()
        {
            string name = Request.Form["userName"];
            DateTime sTime = Convert.ToDateTime(Request.Form["startTime"]);
            DateTime eTime = Convert.ToDateTime(Request.Form["endTime"]);
            int status = Convert.ToInt32(Request.Form["userStatus"]);

            var customers = ProjectHelper.FindProjectBySearch(name, sTime, eTime, status);

            return View(customers);
        }

        [HttpPost]
        [Authorize(Roles = "admin")]
        public ActionResult LockUserAccount() 
        {
            int id = Convert.ToInt32(Request.Form["UserId"]);
            if (ChangeTheStatusInUserAccount(id, 2))
            {
                return GetUserAccount();
            }
            else
            {
                string error = "修改失败！请返回重试哦！";
                return RedirectToAction("Error", "Notice", new { message = error });
            }
        }

        [HttpPost]
        [Authorize(Roles = "admin")]
        public ActionResult UnLockUserAccount()
        {
            int id = Convert.ToInt32(Request.Form["UserId"]);
            if (ChangeTheStatusInUserAccount(id, 1))
            {
                return GetUserAccount();
            }
            else
            {
                string error = "修改失败！请返回重试哦！";
                return RedirectToAction("Error", "Notice", new { message = error });
            }
        }


        [Authorize(Roles = "admin")]
        public ActionResult ProjectListBy(int status)
        {
            var projects = ProjectHelper.FindAllProjectByStatus(status);
            ViewData["ThisProjects"] = projects;
            return View();
        }

        [Authorize(Roles = "admin")]
        public ActionResult ProjectDetails(string pno) 
        {
            var project = ProjectHelper.FindProjectByProjectNo(pno);

            var childProjects = ChildProjectHelper.FindAllChildProjectsTo(pno);

            ViewData["ChildProjects"] = childProjects;

            return View(project);
        }

        [Authorize(Roles = "admin")]
        public ActionResult DoApproveThisProject(int status) 
        {
            string pno = Request.Form["projectNo"];

            //添加一个控制pno和status的输入方法

            bool success = ChangeTheStatusInProject(pno, status);
            if (success)
            {
                return ProjectDetails(pno);
            }
            else
            {
                string msg = "数据访问出错咯！请再试一次吧···";
                return RedirectToAction("Error", "Notice", new { message = msg });
            }
        }


        //修改项目的状态
        public bool ChangeTheStatusInProject(string pno, int status)
        {
            var project = ProjectHelper.FindProjectByProjectNo(pno);
            project.Status = status;
            try
            {
                UpdateModel(project);
                mydb.SaveChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }
       
        //修改用户状态
        public bool ChangeTheStatusInUserAccount(int id, int status)
        {
            var account = mydb.UserAccounts.Find(id);
            account.Status = status;
            try
            {
                UpdateModel(account);
                mydb.SaveChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }


    }
}
